{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "Iter 0,Testing Accuracy 0.8234\n",
      "Iter 1,Testing Accuracy 0.8806\n",
      "Iter 2,Testing Accuracy 0.8998\n",
      "Iter 3,Testing Accuracy 0.9046\n",
      "Iter 4,Testing Accuracy 0.908\n",
      "Iter 5,Testing Accuracy 0.9101\n",
      "Iter 6,Testing Accuracy 0.9122\n",
      "Iter 7,Testing Accuracy 0.9128\n",
      "Iter 8,Testing Accuracy 0.9147\n",
      "Iter 9,Testing Accuracy 0.9167\n",
      "Iter 10,Testing Accuracy 0.9178\n",
      "Iter 11,Testing Accuracy 0.9178\n",
      "Iter 12,Testing Accuracy 0.9193\n",
      "Iter 13,Testing Accuracy 0.9196\n",
      "Iter 14,Testing Accuracy 0.9196\n",
      "Iter 15,Testing Accuracy 0.9202\n",
      "Iter 16,Testing Accuracy 0.9205\n",
      "Iter 17,Testing Accuracy 0.9209\n",
      "Iter 18,Testing Accuracy 0.9209\n",
      "Iter 19,Testing Accuracy 0.9218\n",
      "Iter 20,Testing Accuracy 0.9213\n",
      "Iter 21,Testing Accuracy 0.9228\n",
      "Iter 22,Testing Accuracy 0.9229\n",
      "Iter 23,Testing Accuracy 0.9235\n",
      "Iter 24,Testing Accuracy 0.9228\n",
      "Iter 25,Testing Accuracy 0.9221\n",
      "Iter 26,Testing Accuracy 0.9238\n",
      "Iter 27,Testing Accuracy 0.9235\n",
      "Iter 28,Testing Accuracy 0.9237\n",
      "Iter 29,Testing Accuracy 0.924\n",
      "Iter 30,Testing Accuracy 0.9243\n",
      "Iter 31,Testing Accuracy 0.9243\n",
      "Iter 32,Testing Accuracy 0.9251\n",
      "Iter 33,Testing Accuracy 0.9248\n",
      "Iter 34,Testing Accuracy 0.9242\n",
      "Iter 35,Testing Accuracy 0.9247\n",
      "Iter 36,Testing Accuracy 0.9245\n",
      "Iter 37,Testing Accuracy 0.9254\n",
      "Iter 38,Testing Accuracy 0.9249\n",
      "Iter 39,Testing Accuracy 0.9255\n",
      "Iter 40,Testing Accuracy 0.9248\n",
      "Iter 41,Testing Accuracy 0.9249\n",
      "Iter 42,Testing Accuracy 0.9264\n",
      "Iter 43,Testing Accuracy 0.9268\n",
      "Iter 44,Testing Accuracy 0.9267\n",
      "Iter 45,Testing Accuracy 0.9261\n",
      "Iter 46,Testing Accuracy 0.9261\n",
      "Iter 47,Testing Accuracy 0.9259\n",
      "Iter 48,Testing Accuracy 0.9271\n",
      "Iter 49,Testing Accuracy 0.9267\n",
      "Iter 50,Testing Accuracy 0.9259\n"
     ]
    }
   ],
   "source": [
    "#载入数据集\n",
    "mnist = input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "#每个批次的大小\n",
    "batch_size = 100\n",
    "#计算一共有多少个批次\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "#参数概要\n",
    "def variable_summaries(var):\n",
    "    with tf.name_scope('summaries'):\n",
    "        mean = tf.reduce_mean(var)\n",
    "        tf.summary.scalar('mean', mean)#平均值\n",
    "        with tf.name_scope('stddev'):\n",
    "            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))\n",
    "        tf.summary.scalar('stddev', stddev)#标准差\n",
    "        tf.summary.scalar('max', tf.reduce_max(var))#最大值\n",
    "        tf.summary.scalar('min', tf.reduce_min(var))#最小值\n",
    "        tf.summary.histogram('histogram', var)#直方图\n",
    "\n",
    "#命名空间\n",
    "with tf.name_scope('input'):\n",
    "    #定义两个placeholder\n",
    "    x = tf.placeholder(tf.float32,[None,784],name='x-input')\n",
    "    y = tf.placeholder(tf.float32,[None,10],name='y-input')\n",
    "    \n",
    "with tf.name_scope('layer'):\n",
    "    #创建一个简单的神经网络\n",
    "    with tf.name_scope('wights'):\n",
    "        W = tf.Variable(tf.zeros([784,10]),name='W')\n",
    "        variable_summaries(W)\n",
    "    with tf.name_scope('biases'):    \n",
    "        b = tf.Variable(tf.zeros([10]),name='b')\n",
    "        variable_summaries(b)\n",
    "    with tf.name_scope('wx_plus_b'):\n",
    "        wx_plus_b = tf.matmul(x,W) + b\n",
    "    with tf.name_scope('softmax'):\n",
    "        prediction = tf.nn.softmax(wx_plus_b)\n",
    "\n",
    "#二次代价函数\n",
    "# loss = tf.reduce_mean(tf.square(y-prediction))\n",
    "with tf.name_scope('loss'):\n",
    "    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))\n",
    "    tf.summary.scalar('loss',loss)\n",
    "with tf.name_scope('train'):\n",
    "    #使用梯度下降法\n",
    "    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "#初始化变量\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.name_scope('accuracy'):\n",
    "    with tf.name_scope('correct_prediction'):\n",
    "        #结果存放在一个布尔型列表中\n",
    "        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置\n",
    "    with tf.name_scope('accuracy'):\n",
    "        #求准确率\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "        tf.summary.scalar('accuracy',accuracy)\n",
    "        \n",
    "#合并所有的summary\n",
    "merged = tf.summary.merge_all()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    writer = tf.summary.FileWriter('logs/',sess.graph)\n",
    "    for epoch in range(51):\n",
    "        for batch in range(n_batch):\n",
    "            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)\n",
    "            summary,_ = sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys})\n",
    "            \n",
    "        writer.add_summary(summary,epoch)\n",
    "        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\n",
    "        print(\"Iter \" + str(epoch) + \",Testing Accuracy \" + str(acc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# for i in range(2001):\n",
    "#     #m每个批次100个样本\n",
    "#     batch_xs,batch_ys = mnist.train.next_batch(100)\n",
    "#     summary,_ = sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys})\n",
    "#     writer.add_summary(summary,i)\n",
    "#     if i%500 == 0:\n",
    "#         print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
